package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.exception.SetmealEnableFailedException;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.DishVO;
import com.sky.vo.SetmealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * @author guozh
 */
@Service
public class SetmealServiceImpl implements SetmealService {
    @Autowired
    private SetmealMapper setmealMapper;

    @Autowired
    private SetmealDishMapper setmealDishMapper;

    @Autowired
    private DishMapper dishMapper;

    /**
     * 增加套餐
     *
     * @param setmealDTO 套餐数据
     */
    @Override
    @Transactional
    public void save(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO, setmeal);
        //添加套餐
        setmealMapper.insertSetmeal(setmeal);

        //添加套餐和菜品的连接表
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        if (setmealDishes != null && !setmealDishes.isEmpty()) {
            setmealDishes.forEach(setmealDish -> {
                setmealDish.setSetmealId(setmeal.getId());
            });
            setmealDishMapper.insertSetmealDishes(setmealDishes);
        }
    }

    /**
     * 套餐分页查询
     *
     * @param setmealPageQueryDTO 套餐分页查询数据
     * @return Result<PageResult>
     */
    @Override
    public PageResult pageQuery(SetmealPageQueryDTO setmealPageQueryDTO) {
        PageHelper.startPage(setmealPageQueryDTO.getPage(), setmealPageQueryDTO.getPageSize());
        Page<SetmealVO> page = setmealMapper.selectPageQuery(setmealPageQueryDTO);
        return new PageResult(page.getTotal(), page.getResult());
    }

    /**
     * 批量删除
     *
     * @param ids id
     */
    @Override
    @Transactional
    public void deleteById(List<Long> ids) {
        //起售中的套餐不允许删除
        Setmeal setmeal = setmealMapper.getById(ids);
        if (setmeal.getStatus().equals(StatusConstant.ENABLE)) {
            throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ON_SALE);
        }
        //删除套餐
        setmealMapper.deleteByIds(ids);

        //删除套餐关联菜品
        setmealDishMapper.deleteBySetmealIds(ids);
    }

    /**
     * 根据id查找
     *
     * @param id id
     * @return Result<SetmealVO>
     */
    @Override
    public SetmealVO findById(Long id) {
        List<SetmealDish> setmealDishes = setmealDishMapper.selectBySetmeaId(id);
        Setmeal setmeal = setmealMapper.selectById(id);
        SetmealVO setmealVO = new SetmealVO();
        BeanUtils.copyProperties(setmeal, setmealVO);
        setmealVO.setSetmealDishes(setmealDishes);
        return setmealVO;
    }

    /**
     * 修改套餐
     *
     * @param setmealDTO 修改套餐数据
     */
    @Override
    public void updateSetmeal(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO, setmeal);
        setmealMapper.update(setmeal);

        //删除餐品套餐连接数据
        setmealDishMapper.deleteBySetmealIds(Collections.singletonList(setmeal.getId()));

        //重新写入
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        if (setmealDishes != null && !setmealDishes.isEmpty()) {
            setmealDishes.forEach(setmealDish -> {
                setmealDish.setSetmealId(setmeal.getId());
            });
            setmealDishMapper.insertSetmealDishes(setmealDishes);
        }
    }

    /**
     * 修改状态
     *
     * @param status 状态数据
     * @param id
     */
    @Override
    public void updateStatus(Integer status, Long id) {
        List<Dish> dishes = dishMapper.getBySetmealId(id);
        if (status.equals(StatusConstant.ENABLE)) {
            if (dishes != null && !dishes.isEmpty()) {
                for (Dish dish : dishes) {
                    if (dish.getStatus().equals(StatusConstant.DISABLE)) {
                        throw new SetmealEnableFailedException(MessageConstant.SETMEAL_ENABLE_FAILED);
                    }
                }
            }
        }
        Setmeal setmeal = Setmeal.builder().id(id).status(status).build();
        setmealMapper.update(setmeal);
    }

    /**
     * 根据分类id查询套餐
     *
     * @param categoryId 分类id
     * @return Result<List <SetmealVO>>
     */
    @Override
    public List<SetmealVO> selectSetmealList(Long categoryId) {
        Setmeal setmeal = Setmeal.builder().categoryId(categoryId).status(1).build();
        return setmealMapper.selectByCategoryId(setmeal);
    }

    /**
     * 根据套餐id查询包含的菜品
     *
     * @param id id
     * @return Result<SetmealVO>
     */
    @Override
    public List<SetmealDish> getBySetmealId(Long id) {
        return setmealDishMapper.selectBySetmeaId(id);
    }
    /**
     * 根据套餐id查询包含的菜品
     *
     * @param id id
     * @return Result<SetmealVO>
     */
    @Override
    public List<DishItemVO> getDishItemById(Long id) {
        return setmealMapper.getDishItemBySetmealId(id);
    }
}
